/**
 * 
 * @author ZhangHuihua@msn.com
 * @param {Object} opts Several options
 */
(function($){
	$.fn.pagination = function(opts){
		var setting = {
			first$:"li.j-first", prev$:"li.j-prev", next$:"li.j-next", last$:"li.j-last", nums$:"li.j-num>a", jumpto$:"li.jumpto",
			pageNumFrag:'<li class="#liClass#"><a>#pageNum#</a></li>',
			numPerPage:'<option value="#pageNum#" #selected#>#pageNum#</option>',
			
			numPerPageList:[5,10,20,30,50,80,100]
		};
		return this.each(function(){
			var $this = $(this);
			
			opts=$.extend({
				totalCount:parseInt($this.attr("totalCount")||0),
				numPerPage:parseInt($this.attr("numPerPage")||20),
				pageNumShown:parseInt($this.attr("pageNumShown")||10),
				currentPage:parseInt($this.attr("currentPage")||1)
			},opts);
			
			var pc = new Pagination(opts);
			_init($this, pc);
		});
		
		function _init($this,pc){
			$this.html("");
			var interval = pc.getInterval();
			var pageNumFrag = '';
			for (var i=interval.start; i<interval.end;i++){
				pageNumFrag += setting.pageNumFrag.replaceAll("#pageNum#", i).replaceAll("#liClass#", i==pc.getCurrentPage() ? 'selected j-num' : 'j-num');
			}
			
			var pages = $this.prev(".pages");
			pages.html(DWZ.frag["pages"].replaceAll("#numPages#",pc.numPages()).replaceAll("#totalCount#",pc.totalCount()));
			
			var select=$("select",pages);
			var numPerPage = '';
			for (var i=0; i<setting.numPerPageList.length;++i){
				numPerPage += setting.numPerPage.replaceAll("#pageNum#", setting.numPerPageList[i])
						.replaceAll("#selected#", setting.numPerPageList[i]==pc.numPerPage() ? 'selected' : '');
			}
			select.html(numPerPage);
			select.change(function(){
				$this.attr("numPerPage",this.options[this.selectedIndex].value).attr("currentPage",1);
				//DWZ.loadPageContent($this);
				var table=$this.data("table");
				if(table){
					table.jLoad();
				}else{
					DWZ.loadPageContent($this);
				}
			});
			
			
			$this.attr("currentPage",pc.getCurrentPage()).attr("numPerPage",pc.numPerPage());
			$this.html(DWZ.frag["pagination"].replaceAll("#pageNumFrag#", pageNumFrag)
											.replaceAll("#currentPage#", pc.getCurrentPage()))
											.find("li").hoverClass();
			
			var $first = $this.find(setting.first$);
			var $prev = $this.find(setting.prev$);
			var $next = $this.find(setting.next$);
			var $last = $this.find(setting.last$);
			
			if (pc.hasPrev()){
				$first.add($prev).find(">span").hide();
				_bindEvent($this, pc, $prev, pc.getCurrentPage()-1, pc.targetType());
				_bindEvent($this, pc, $first, 1, pc.targetType());
			} else {
				$first.add($prev).addClass("disabled").find(">a").hide();
			}
			
			if (pc.hasNext()) {
				$next.add($last).find(">span").hide();
				_bindEvent($this, pc, $next, pc.getCurrentPage()+1, pc.targetType());
				_bindEvent($this, pc, $last, pc.numPages(), pc.targetType());
			} else {
				$next.add($last).addClass("disabled").find(">a").hide();
			}

			$this.find(setting.nums$).each(function(i){
				if(i+1==pc.getCurrentPage())return;
				_bindEvent($this, pc, $(this), i+interval.start, pc.targetType());
			});
			
			$this.find(setting.jumpto$).each(function(){
				var $jumpto = $(this);
				var $inputBox = $jumpto.find(":text");
				var $button = $jumpto.find(":button");
				$button.click(function(event){
					var pageNum = $inputBox.val();
					if (pageNum && pageNum.isPositiveInteger()) {
						if (pc.targetType() == "dialog") {
							dialogPageBreak({pageNum:pageNum,pageNo:pageNum});
						} else if (pc.targetType == "navTab") {
							navTabPageBreak({pageNum:pageNum,pageNo:pageNum});
						}else{
							pc.opts.currentPage=pageNum;
							_init($this,pc);
							var table=$this.data("table");
							if(table){
								table.jLoad();
							}else{
								DWZ.loadPageContent($this);
							}
						}
					}
				});
				$inputBox.keyup(function(event){
					if (event.keyCode == DWZ.keyCode.ENTER) $button.click();
				});
			});
		}
		
		function _getPagination(jTarget){
	 		return jTarget.parents(".pageContent").find(".pagination");
	 	}
		
		function _bindEvent($this, pc, jTarget, pageNum, targetType){
			jTarget.bind("click", {pageNum:pageNum}, function(event){
				if (targetType == "dialog") {
					dialogPageBreak({pageNum:event.data.pageNum,pageNo:event.data.pageNum});
				} else if (targetType == "navTab") {
					navTabPageBreak({pageNum:event.data.pageNum,pageNo:event.data.pageNum});
				}else{
					pc.opts.currentPage=pageNum;
					_init($this,pc);
					var table=$this.data("table");
					if(table){
						table.jLoad();
					}else{
						DWZ.loadPageContent($this);
					}
				}
				event.preventDefault();
			});
		}
	}

	var Pagination = function(opts) {
		this.opts = $.extend({
			//targetType:"navTab",	// navTab, dialog, null
			totalCount:0,			//总数
			numPerPage:20,			//每页显示20条记录
			pageNumShown:10,		//显示10个页码
			currentPage:1,			//当前页
			callback:function(){return false;}
		}, opts);
	}
	
	$.extend(Pagination.prototype, {
		numPerPage:function(){return this.opts.numPerPage},
		targetType:function(){return this.opts.targetType},
		totalCount:function(){
			return this.opts.totalCount;
		},
		numPages:function() {
			return Math.ceil(this.opts.totalCount/this.opts.numPerPage);
		},
		getInterval:function(){
			var ne_half = Math.ceil(this.opts.pageNumShown/2);
			var np = this.numPages();
			var upper_limit = np - this.opts.pageNumShown;
			var start = this.getCurrentPage() > ne_half ? Math.max( Math.min(this.getCurrentPage() - ne_half, upper_limit), 0 ) : 0;
			var end = this.getCurrentPage() > ne_half ? Math.min(this.getCurrentPage()+ne_half, np) : Math.min(this.opts.pageNumShown, np);
			return {start:start+1, end:end+1};
		},
		getCurrentPage:function(){
			var currentPage = parseInt(this.opts.currentPage);
			if (isNaN(currentPage)) return 1;
			if(currentPage>this.numPages()) currentPage=this.numPages();
			return currentPage||1;
		},
		hasPrev:function(){
			return this.getCurrentPage() > 1;
		},
		hasNext:function(){
			return this.getCurrentPage() < this.numPages();
		}
	});
})(jQuery);
